package com.woniuxy.propertyjava.controller;



import com.woniuxy.propertyjava.entity.Accesscard;
import com.woniuxy.propertyjava.entity.PageBean;
import com.woniuxy.propertyjava.entity.ResponseResult;
import com.woniuxy.propertyjava.service.AccesscardService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (Accesscard)表控制层
 *
 * @author makejava
 * @since 2022-07-06 09:59:27
 */
@RestController
@RequestMapping
public class AccesscardController {
    /**
     * 服务对象
     */
    @Resource
    AccesscardService accesscardService;

    /**
     * 分页查询
     *
     * @param accesscard  筛选条件
     * @param pageIndex,pageSize 分页对象
     * @return 查询结果
     */
    @PostMapping("/accesscard/{pageIndex}/{pageSize}")
    public ResponseResult<PageBean<Accesscard>> queryByPage(@RequestBody Accesscard accesscard,
                                                            @PathVariable("pageIndex") int pageIndex,
                                                            @PathVariable("pageSize") int pageSize) {
        PageBean<Accesscard> pageBean = accesscardService.queryByPage(accesscard, pageIndex, pageSize);

        return new ResponseResult<>(200,"ok",pageBean);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/accesscard/{id}")
    public ResponseResult<Accesscard> queryById(@PathVariable("id") Integer id) {
        Accesscard query = accesscardService.queryById(id);
        return new ResponseResult<>(200,"ok",query);
    }

    /**
     * 新增数据
     *
     * @param accesscard 实体
     * @return 新增结果
     */
    @PostMapping("/accesscard")
    public ResponseResult<Accesscard> add(@RequestBody Accesscard accesscard) {

        Accesscard queryById = accesscardService.queryById(accesscardService.insert(accesscard).getId());
        return new ResponseResult<>(200,"ok",queryById);
    }

    /**
     * 编辑数据
     *
     * @param accesscard 实体
     * @return 编辑结果
     */
    @PutMapping("/accesscard")
    public ResponseResult<Accesscard> edit(@RequestBody Accesscard accesscard) {
        Accesscard queryById = accesscardService.queryById(accesscardService.update(accesscard).getId());

        return new ResponseResult<>(200,"ok",queryById);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/accesscard/{id}")
    public ResponseResult deleteById(@PathVariable Integer id) {
        accesscardService.deleteById(id);
        return new ResponseResult(200,"ok","");
    }

}

